/*
 * @Author: 李石
 * @Date: 2024-05-22 10:55:16
 * @LastEditors: lishi
 * @LastEditTime: 2024-05-22 10:56:24
 * @Description: 
 * Copyright (c) 2024 by ${lishi0105@163.com}, All Rights Reserved. 
 */
#ifndef _TIMER_UTIL_HPP
#define _TIMER_UTIL_HPP

#include <functional>
#include <thread>
#include <atomic>
#include <sys/timerfd.h>
#include <unistd.h>
#include <poll.h>
#include <iostream>
#include "ThreadPool.h" // 假设ThreadPool.h和ThreadPool.cpp都在同一目录

class TimerUtil {
public:
    using TimerCallback = std::function<void()>;

    TimerUtil(size_t maxThreads = 5);
    ~TimerUtil();

    void start(int initial_usec, int interval_usec, TimerCallback callback);
    void stop();

private:
    void run();

    int timer_fd;
    std::thread timer_thread;
    std::atomic<bool> running;
    TimerCallback callback;
    std::unique_ptr<ThreadPool> threadPool;
    std::atomic<size_t> activeTasks;
    size_t maxThreads;
};

#endif // _TIMER_UTIL_HPP
